<template>
	<!-- 六月总计账单 -->
	<view class="content">
		<scroll-view @scrolltolower="scrolltolower" scroll-y class="content flex-h-1">
			<view class="header">
				<view class="header_content">
					<view class="header_6">
						订单总额
					</view>
					<view class="header_moneys">
						{{sumMoney}}
					</view>
					<view class="header_zheng">
						入账周期：整月
					</view>
				</view>
			</view>
			<view class="mt24">
				<view class="thisMonth" v-for="(item,index) in newList" :key="index">
					<view class="thisMonth_header">
						{{item[0].day}}
					</view>
					<view class="" v-for="(item1,index1) in item" :key="index1">
						<!-- 调取接口订单获取 -->
						<view class="orderList">
							<view class="">
								<view class="gouwu">
									<view v-if="item1.type==0" class="gouwu_name">
										商城购物
									</view>
									<view v-if="item1.type==1" class="gouwu_name">
										退款
									</view>
									<view v-if="item1.type==2" class="gouwu_name">
										还款
									</view>
									<view class="gouwu_money">
										{{item1.money}}
									</view>
								</view>
								<view class="mt14 orBian">
								{{item1.order_no}}
								</view>
								<view class="mt10 orBian">
									{{timestampToTime(item1.updated_at)}}
								</view>
								<view class="xian"></view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="more"></u-loadmore>
		</scroll-view>
	</view>
</template>

<script>
	import monthlyPaymentA from "@/api/monthlyPaymentA.js"
	export default {
		data() {
			return {
				month: '',
				newMonth: '',
				list: [],
				obj: {},
				page: 1,
				more: 'loadmore',
				sumMoney: '',
				newList:[]
			}
		},
		onLoad(option) {
			this.month = option.time
			this.newMonth = parseInt(option.month, 10).toString();
			
			// 动态改变标题月份
			uni.setNavigationBarTitle({
				title: `${this.newMonth}月总计账单`
			});
			this.getList()
		},
		methods: {
			// 获取初始化数据
			getList() {
				if (this.more == 'nomore') return;
				monthlyPaymentA
					.getPayment({
						month:this.month,
						page: this.page,
					})
					.then((res) => {
						if (res.code == 0) {
							this.sumMoney=res.sumMoney
							this.obj = res.user_info
							this.list.push(...res.data.list);
							this.more =this.list.length >= res.data.totalCount ? 'nomore' : 'loadmore';
							let groupedArrays = this.list.reduce((acc, obj,index) => {
							  // 获取当前对象的day属性值
							  let day = obj.day;
							
							  // 检查结果累加器中是否已经有该属性的数组，如果没有则创建一个新数组
							  if (!acc[day]) {
							    acc[day] = [];
							  }
							
							  // 将当前对象推入相应的数组中
							  obj.day = day; // 将day属性值添加到对象中
							  acc[day].push(obj);
							  
							  // 返回累加器
							  return acc;
							}, {});
							
							let newIndex = 0;
							let newObj = {};
							Object.keys(groupedArrays).forEach(key => {
							  newObj[newIndex++] = groupedArrays[key];
							});
							this.newList=newObj 
						}
					});
			},
			// 触发底部
			scrolltolower() {
				this.page++;
				this.getList()
			},
			timestampToTime(timestamp) {
				// 将时间戳转换为毫秒
				let time = new Date(timestamp * 1000);
				// 使用 Date 对象的方法获取年、月、日、时、分、秒
				let year = time.getFullYear();
				let month = time.getMonth() + 1; // 月份是从0开始的，所以要加1
				let day = time.getDate();
				let hours = time.getHours();
				let minutes = time.getMinutes();
				let seconds = time.getSeconds();
			
				// 返回格式化后的时间字符串
				return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
			},
		}
	}
</script>

<style>
	.mt24 {
		margin-top: 48rpx;
	}

	.thisMonth_header {
		padding: 24rpx;
		font-weight: 400;
		font-size: 30rpx;
		color: #8C8C8C;
	}

	.xian {
		width: 654rpx;
		height: 2rpx;
		background: #F8F8F8;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		margin: 30rpx auto;
	}

	.orBian {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 24rpx;
		color: #8C8C8C;
		font-style: normal;
		text-transform: none;
	}

	.mt14 {
		margin-top: 14rpx;
	}

	.mt10 {
		margin-top: 10rpx;
	}

	.gouwu {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.gouwu_money {
		font-weight: 600;
		font-size: 34rpx;
		color: #333333;
	}

	.gouwu_name {
		font-weight: 400;
		font-size: 28rpx;
		color: #262626;
	}

	.orderList {
		padding: 0rpx 24rpx;
	}

	.selsecaLL {
		font-weight: 400;
		font-size: 24rpx;
		color: #8C8C8C;
	}

	.ml10 {
		margin-left: 10rpx;
	}

	.thisMonth {
		width: 702rpx;
		margin: 24rpx 24rpx auto;
		background-color: #FFF;
		font-family: PingFang SC, PingFang SC;
		font-style: normal;
		text-transform: none;
		border-radius: 20rpx 20rpx 20rpx 20rpx;

	}

	.content {
		min-height: 100vh;
		background-color: #F2F2F2;
	}

	.header {
		width: 702rpx;
		margin: 0rpx auto;
		background-color: #FFF;
		font-family: PingFang SC, PingFang SC;
		font-style: normal;
		text-transform: none;
		border-radius: 20rpx 20rpx 20rpx 20rpx;
		position: relative;
		top: 24rpx;
	}

	.header_6 {
		font-weight: 400;
		font-size: 30rpx;
		color: #262626;
	}

	.header_moneys {
		font-weight: 600;
		font-size: 60rpx;
		color: #262626;
	}

	.header_zheng {
		font-weight: 400;
		font-size: 24rpx;
		color: #8C8C8C;
	}

	.header_content {
		padding: 24rpx;
	}
</style>